//
//  ELAnnotationDetailView.swift
//  WZApp
//
//  Created by evol on 2018/7/30.
//  Copyright © 2018年 evol. All rights reserved.
//

import UIKit
import RxSwift

class ELAnnotationDetailView: UIButton {
    
    let disposeBag = DisposeBag()
    var bgImageView: UIImageView!
    var viewModel: ELAnnotationDetailVM!
    
    init(_ viewModel: ELAnnotationDetailVM) {
        super.init(frame: UIScreen.main.bounds)
        self.viewModel = viewModel
        let bgImage = UIImage(named: "icon_annotation_info_bg")?.stretchableImage(withLeftCapWidth: 60, topCapHeight: 60)
        bgImageView = UIImageView(image: bgImage)
        bgImageView.isUserInteractionEnabled = true
        self.addSubview(bgImageView)
        bgImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
            make.size.equalTo(imageSize())
        }
        
        let scrollView = UIScrollView()
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        bgImageView.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalTo(bgImageView).inset(UIEdgeInsetsMake(42, 35, 42, 35))
        }
        
        let contentView = UIView()
        scrollView.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.edges.equalTo(scrollView)
            make.width.equalTo(scrollView).offset(-0.1)
        }
        
        let upIcon = UIImageView(image: UIImage(named: "action_show_icon_up"))
        let downIcon = UIImageView(image: UIImage(named: "action_show_icon_down"))
        contentView.addSubview(upIcon)
        contentView.addSubview(downIcon)
        
        // Title
        let titleLabel = ELVerticalFormLabel(text: viewModel.model.title, font: UIFont.systemFont(ofSize: 25), textColor: UIColor.hexColor("693200"), space: 0)
        contentView.addSubview(titleLabel)
        upIcon.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.right.equalTo(-8)
        }
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(upIcon)
            make.top.equalTo(upIcon.snp.bottom).offset(5)
            make.size.equalTo(titleLabel.frame.size)
        }
        downIcon.snp.makeConstraints { (make) in
            make.centerX.equalTo(upIcon)
            make.top.equalTo(titleLabel.snp.bottom).offset(5)
        }
        //  Content
        let infoContentView = ELAnnotationDetailContentView(text: viewModel.model.content, font: UIFont.systemFont(ofSize: 13), textColor: UIColor.hexColor("693200"), space: 8) { (height) in
        }
        contentView.addSubview(infoContentView)
        
        //  Line
        let lineView = UIView()
        lineView.backgroundColor = UIColor.hexColor("693200")
        contentView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.right.equalTo(upIcon.snp.left).offset(-14)
            make.top.equalTo(0)
            make.width.equalTo(0.5)
            make.height.equalTo(infoContentView)
        }
        
        infoContentView.snp.makeConstraints { (make) in
            make.right.equalTo(lineView.snp.left).offset(-14)
            make.top.equalTo(0)
            make.size.equalTo(infoContentView.frame.size)
        }
        contentView.snp.makeConstraints { (make) in
            make.bottom.greaterThanOrEqualTo(infoContentView)
        }
        
        // Button
        let navigateButton = UIButton()
        navigateButton.setBackgroundImage(UIImage(named: "icon_annotation_info_button"), for: .normal)
        navigateButton.setTitle("导航", for: .normal)
        navigateButton.setTitleColor(.white, for: .normal)
        navigateButton.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        bgImageView.addSubview(navigateButton)
        navigateButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(bgImageView)
            make.bottom.equalTo(-15)
        }
        
        navigateButton.rx.tap.subscribe(onNext:{[weak self] button in
            self?.viewModel.navigateCommand.onNext(0)
            self?.close()
        }).disposed(by: disposeBag)
        
        //自己的点击时间 close
        self.rx.tap.subscribe(onNext:{
            [weak self] button in
            self?.close()
        }).disposed(by: self.disposeBag)
    }
    
    func close() -> Void {
        UIView.animate(withDuration: 0.25, animations: {
            self.bgImageView.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
            self.bgImageView.alpha = 0
        }, completion: { (finished) in
            if finished {
                self.removeFromSuperview()
            }
        })
    }
    
    func imageSize() -> CGSize {
        switch self.viewModel.model.type {
        case 1:
            return CGSize(width: 364.5, height: 505)
        case 2:
            return CGSize(width: 300, height: 416)
        case 3:
            return CGSize(width: 240, height: 343.5)
        default:
            return CGSize(width: 0, height: 0)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
